$(function(argument) {
	var region_id;
	var region_name;
	var mall_id;
	var lng1;
	var lat1;
	var cat_id;
	var brand_id;
	region_id = window.sessionStorage.getItem("region_id");
	// console.log(region_id);
	region_name = window.sessionStorage.getItem("region_name");
	if (region_id == null) {
		region_id = 52;
		$(".currentCity").html("北京");
	} else {
		$(".currentCity").html(region_name);
	}
	// 获取定位方法
	function getLcation(callback) {
		var map = new BMap.Map("allmap");
		var geolocation = new BMap.Geolocation();
		geolocation.getCurrentPosition(function(r) {
			if (this.getStatus() == BMAP_STATUS_SUCCESS) {
				var mk = new BMap.Marker(r.point);
				map.addOverlay(mk);
				map.panTo(r.point);
				lng1 = r.point.lng;
				lat1 = r.point.lat;
				// console.log(lng1);
				// console.log(lat1);
				callback();
				// alert('您的位置：' + r.point.lng + ',' + r.point.lat);
			} else {
				alert('failed' + this.getStatus());
			}
		}, {
			enableHighAccuracy: true
		})

	}
	// 距离限定
	template.helper("meter", function(a) {
			var meter = (Number(a) / 1000).toFixed(2);
			return meter;
		})
		// 切换城市
	$(".currentCity").click(function() {
			window.location.href = "selectCity.html";
			window.sessionStorage.setItem("backOne", 1);
		})
		// 点击进入商家详情
	$(document).on("click", ".shops", function() {
			var sid = $(this).eq(0).attr('data-seller_id');
			window.sessionStorage.setItem("sid", sid);
			window.location.href = "waterBusinessperson.html";
		})
		// 点击返回
	$(".titleTea a").click(function(e) {
			e.preventDefault();
			window.location.href = "../index.html";
			window.sessionStorage.removeItem("region_id");
			window.sessionStorage.removeItem("region_name");
		})
		// 渲染筛选项方法
	function showScreen(mall_id) {
		$.ajax({
				url: 'http://www.teaweilai.com/app/Admin/Mallcat/CategoryShow',
				type: 'POST',
				dataType: 'json',
				data: {
					mall_id: mall_id
				}
			})
			.done(function(data) {
				// console.log(data);
				var screenStr = '';
				for (var i = 0; i < data.sing.length; i++) {
					if (data.sing[i].cat_name == "水") {
						data.sing[i].cat_name = "送水平台"
					}
					if (data.sing[i].cat_name == "桶装水") {
						data.sing[i].cat_name = "品牌水"
					}
					screenStr += '<button class="sBtn screen' + data.sing[i].cat_id + '" data_id=' + data.sing[i].cat_id + '>' + data.sing[i].cat_name + '</button>';
				}
				$(".screenBtn").html(screenStr);

			})
			.fail(function() {
				console.log("error");
			})

	}
	// 商户列表渲染
	function showShop(mall_id) {
		$.ajax({
				url: 'http://www.teaweilai.com/app/Admin/Mallcat/EachMeter',
				type: 'POST',
				dataType: 'json',
				data: {
					region_id: region_id,
					lng1: lng1,
					lat1: lat1,
					mall_id: mall_id
				},
			})
			.done(function(data) {
				// console.log(data);
				if (data.code == 500) {
					$(".shopList").html('<h2 style="text-align:center;line-height:5rem">暂未开放敬请期待...</h2>');
					$(".screenBtn").hide();
					$(".waterBrand").hide();
				}
				if (data.code == 200) {
					$(".waterBrand").show();
					$(".screenBtn").show();
					var shopListStr = template("shopL", {
						mall: data
					})
					$(".shopList").html(shopListStr);
				}
			})
			.fail(function() {
				console.log("error");
			})
			.always(function() {});
	}

	// 首次进入页面渲染总分类
	$.ajax({
			url: 'http://www.teaweilai.com/app/Admin/Mallcat/CatShow',
			type: 'POST',
			dataType: 'json'
		})
		.done(function(data) {
			// console.log(data);
			var firstStr = template("firstSystem", {
				first: data.sing
			})
			$(".container").html(firstStr);
			// 进来渲染页面，买水送茶分类下面的数据			
			getLcation(function() {
				showShop(6);
			})
			showScreen(6)
		})
		.fail(function() {
			console.log("error");
		})
		// 点击切换一级分类
	$(document).on("click", ".firstS", function() {
			mall_id = $(this).attr('data_cat_id');
			showShop(mall_id);
			showScreen(mall_id)
		})
		// 点击品牌水分类
	$(document).on("click", ".sBtn", function() {
		// console.log($(this));
		$(this).siblings('button').removeClass('btnPitchOn');
		$(this).addClass('btnPitchOn');
		cat_id = $(this).attr("data_id");
		if ($(this).attr("data_id") == 1496) {
			var that = $(this);
			$.ajax({
					url: 'http://www.teaweilai.com/app/Admin/Mallcat/PingMater',
					type: 'POST',
					dataType: 'json',
					data: {
						cat_id: cat_id,
						region_id: region_id
					},
				})
				.done(function(data) {
					// console.log(data);
					if (data.sing == 0) {
						getScreenList()
					} else if (data.sing == 3) {
						$(".shopList").html('<h2 style="text-align:center;line-height:5rem">暂未开放敬请期待...</h2>');
					} else {
						var brandStr = "";
						for (var i = 0; i < data.sing.length; i++) {
							brandStr += '<button class="bBtn"  data_brand_id=' + data.sing[i].brand_id + '>' + data.sing[i].brand_name + '</button>';
						};
						$(".waterBrand").html(brandStr);
						getScreenList(data.sing[0].brand_id);
					}
				})
				.fail(function() {
					console.log("error");
				})
		} else {
			$(".waterBrand").html('');
			getScreenList();
		}
	})

	// 获取筛选之后的list
	function getScreenList(brand_id) {
		$.ajax({
				url: 'http://www.teaweilai.com/app/Admin/Mallcat/PingShow',
				type: 'POST',
				dataType: 'json',
				data: {
					brand_id: brand_id,
					cat_id: cat_id,
					lng1: lng1,
					lat1: lat1,
					region_id: region_id
				},
				beforeSend: function() {
					console.log(brand_id);
				}
			})
			.done(function(data) {
				console.log(data);
				if (data.code == 500) {
					$(".shopList").html('<h2 style="text-align:center;line-height:5rem">暂无合作商家敬请期待...</h2>');
				} else {
					var shopListStr = template("shopL", {
						mall: data
					})
					$(".shopList").html(shopListStr);
				}
				// callback();

			})
			.fail(function() {
				console.log("error");
			})
	}
	// 
	$(document).on("click", ".bBtn", function() {
		$(this).siblings('button').removeClass('btnPitchOn');
		$(this).addClass('btnPitchOn');
		brand_id = $(this).attr("data_brand_id");
		getScreenList(brand_id);

	})

})